//
//  XiaoximenPerson.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-19.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenPerson.h"

@interface XiaoximenPerson ()

@end

@implementation XiaoximenPerson
@synthesize party, ID, phone, name, isVoted, isComing;
@synthesize placeVotes = _placeVotes;
@synthesize timeVotes = _timeVotes;

- (id)initWithPhoneString:(NSString *)phoneString
				 userName:(NSString *)userName
				 personID:(NSUInteger)personID
{
	self = [super init];
	if (self) {
        // Custom initialization
		phone = phoneString;
		name = userName;
		ID = personID;
		isVoted = NO;
		isComing = NO;
	}
	return self;
}
- (NSMutableDictionary *)placeVotes
{
	if (_placeVotes == nil) {
		_placeVotes = [[NSMutableDictionary alloc] init];
	}
	return _placeVotes;
}
- (NSMutableDictionary *)timeVotes
{
	if (_timeVotes == nil) {
		_timeVotes = [[NSMutableDictionary alloc] init];
	}
	return _timeVotes;
}

- (void)yesForPlaceID:(NSUInteger)placeID
{
	self.isVoted = YES;
	self.isComing = YES;
	[self.placeVotes setValue:@"YES" forKey:[NSString stringWithFormat:@"%d", placeID]];
	XiaoximenParty *parentParty = self.party;
	for (XiaoximenVotePlace * place in parentParty.places) {
		if (place.ID == placeID) {
			place.totalVote++;
			place.yesVote++;
			[place.yesPersons addObject:[NSString stringWithFormat:@"%d", self.ID]];
		}
	}
}
- (void)noForPlaceID:(NSUInteger)placeID
{
	self.isVoted = YES;
	[self.placeVotes setValue:@"NO" forKey:[NSString stringWithFormat:@"%d", placeID]];
	XiaoximenParty *parentParty = self.party;
	for (XiaoximenVotePlace * place in parentParty.places) {
		if (place.ID == placeID) {
			place.totalVote++;
			place.noVote++;
			[place.noPersons addObject:[NSString stringWithFormat:@"%d", self.ID]];
		}
	}
}
- (void)yesForTimeID:(NSUInteger)timeID
{
	self.isVoted = YES;
	self.isComing = YES;
	[self.timeVotes setValue:@"YES" forKey:[NSString stringWithFormat:@"%d",timeID]];
	XiaoximenParty *parentParty = self.party;
	for (XiaoximenVoteTime * time in parentParty.timeIntervals) {
		if (time.ID == timeID) {
			time.totalVote++;
			time.yesVote++;
			[time.yesPersons addObject:[NSString stringWithFormat:@"%d", self.ID]];
		}
	}
}
- (void)noForTimeID:(NSUInteger)timeID
{
	self.isVoted = YES;
	[self.timeVotes setValue:@"NO" forKey:[NSString stringWithFormat:@"%d",timeID]];
	XiaoximenParty *parentParty = self.party;
	for (XiaoximenVoteTime * time in parentParty.timeIntervals) {
		if (time.ID == timeID) {
			time.totalVote++;
			time.noVote++;
			[time.noPersons addObject:[NSString stringWithFormat:@"%d", self.ID]];
		}
	}
}

@end
